package com.liu.demo.webapi;

import com.liu.demo.pojo.Student;
import com.liu.demo.pojo.User;
import com.liu.demo.service.StudentService;
import com.liu.demo.service.UserService;
import com.liu.demo.utils.PageUtils;
import com.liu.demo.utils.RUtils;
import com.liu.demo.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.web.bind.annotation.*;
import org.apache.commons.lang.StringUtils;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@CrossOrigin(origins = "*")
@RequestMapping("/webapi/student")
public class StudentRestController {

    @Autowired
    public StudentService studentService;
    @Autowired
    public UserService userService;

    @GetMapping("/login")
    public R stuLogin(HttpServletRequest httpServletRequest, Integer username, String password) throws Exception{
        User loginRes = userService.login(username,password);

        if(loginRes != null){
            HttpSession session = httpServletRequest.getSession();
            session.setAttribute("user",loginRes.getUsername());
            return RUtils.success(loginRes);
        }else{
            return RUtils.error(-1,"帐号或密码错误");
        }

    }

    @GetMapping("/list")
    public R getAll(){
        List<Student> students = studentService.findAll();
        return RUtils.success(students);
    }

    @RequestMapping("/findByName")
    public List<Student> findByName(@RequestBody String name){
        List<Student> students1 = studentService.findByName(name);
        return  students1;
    }

    @PostMapping("/insertOne")
    public R insertOne(Student student){
        studentService.insertOne(student);
        return RUtils.success(200,"更新信息成功！");
    }

    @PutMapping("/updateOne")
    public void updateOne(Student student){
        studentService.updateOne(student);
    }

    @DeleteMapping("/deleteOne")
    public R deleteOne(Student student){
        studentService.deleteOne(student);
        return RUtils.success(200,"删除成功！");
    }

    @GetMapping("/getStu/{id}")
    public R get(@PathVariable Integer id){
        Student student = studentService.findByIdLike(id);
        return RUtils.success(student);
    }

    @GetMapping("/getByPage")
    public PageUtils getByPage(@RequestParam(value = "page",defaultValue = "0") Integer page,
                               @RequestParam(value = "size",defaultValue = "50") Integer size,
                               @RequestParam(value = "name",defaultValue = "") String name,
                               @RequestParam(value = "sex",defaultValue = "") String sex){

        Page <Student> studentPage;
        if(StringUtils.isEmpty(name) && StringUtils.isEmpty(sex)){
            //按id倒序
            Pageable pageable = PageRequest.of(page,size,Sort.by("id").descending());
            studentPage = studentService.findAll(pageable);
        }else if(StringUtils.isNotEmpty(sex) && StringUtils.isEmpty(name)){
            Student student = new Student();
            student.setSex(sex);

            Pageable pageable = PageRequest.of(page,size,Sort.by("id").descending());
            ExampleMatcher matcher = ExampleMatcher.matching()
                    .withMatcher("sex",ExampleMatcher.GenericPropertyMatchers.contains());
            Example<Student> example = Example.of(student,matcher);

            studentPage = studentService.findAll(example,pageable);
        }else if(StringUtils.isNotEmpty(name) && StringUtils.isEmpty(sex)){
            Student student = new Student();
            student.setName(name);

            Pageable pageable = PageRequest.of(page,size,Sort.by("id").descending());
            ExampleMatcher matcher = ExampleMatcher.matching()
                    .withMatcher("name",ExampleMatcher.GenericPropertyMatchers.contains());
            Example<Student> example = Example.of(student,matcher);

            studentPage = studentService.findAll(example,pageable);
        }else {
            Student student = new Student();
            student.setName(name);
            student.setSex(sex);

            Pageable pageable = PageRequest.of(page,size,Sort.by("id").descending());
            ExampleMatcher matcher = ExampleMatcher.matching()
                    .withMatcher("name",ExampleMatcher.GenericPropertyMatchers.contains())
                    .withMatcher("sex",ExampleMatcher.GenericPropertyMatchers.contains());
            Example<Student> example = Example.of(student,matcher);

            studentPage = studentService.findAll(example,pageable);
        }


        PageUtils pageUtils = new PageUtils(studentPage.getContent(),studentPage.getTotalElements());
        return pageUtils;
    }

}
